package	com.mes.web.controller.jyy;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mes.entity.jyy.Approve;
import com.mes.entity.jyy.ApproveDetail;
import com.mes.entity.jyy.DepAndStaff;
import com.mes.entity.tdd.Staff;
import com.mes.service.jyy.ApproveDetailService;
import com.mes.service.jyy.ApproveService;
import com.mes.utils.DateJsonValueProcessor;
import com.mes.utils.ResponseUtil;
/**
 *
 *@ClassName: ApproveController
 *@Description: 
 *@Author jyy
 *@Date 2019-07-22 11:18:08
 *@version 1.0
*/
@Controller
@RequestMapping("/approve")
public class ApproveController{
	@Resource ApproveService approveService;
	@Resource ApproveDetailService approveDetailService;

	/**
	 * 查询所有的部门和员工
	 */
	@RequestMapping("/selectAllDepAndStaff")
	@ResponseBody
	public List<DepAndStaff> selectAllDepAndStaff() {
		List<DepAndStaff> list = approveService.selectAllDepAndStaff();
		return list;
	}
	
	/** 
	 * 根据主键id查询实体
	 * @param id 主键id
	 * @return 实体
	*/
	@RequestMapping("/queryById")
	@ResponseBody
	public Approve queryById(Long id) {
		Approve param = approveService.queryById(id);
		return param;
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/queryAllByMution")
	@ResponseBody
	public List<Approve> queryAllByMution(Approve param) {
		List<Approve> list = approveService.queryAllByMution(param);
		return list;
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/list")
	public void list(Approve param,HttpServletResponse response) throws Exception{
		/*
		 * 条件分页查询内容
		 */
		List<Approve> list = approveService.findPageByMution(param);
		/*
		 * 条件查询总条数
		 */
		Integer total = approveService.selectCountByMution(param);
		/*
		 * 转为json数据传至页面
		 */
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
		//创建result
		JSONObject result = new JSONObject();
		//转json数据
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		//添加入result
		result.put("code", 0);
		result.put("data", jsonArray);
		result.put("count", total);
		ResponseUtil.write(response, result);
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/insert")
	public void insert(Approve param,Long[] staffids,HttpSession session,HttpServletResponse response) throws Exception{
        //从session中取出用户身份信息
        Staff userSession = (Staff) session.getAttribute("staff");
        //当前时间
        SimpleDateFormat   sDateFormat   =   new   SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
        String   date   =   sDateFormat.format(new Date());
		
		param.setState(0);
		param.setPeople(userSession.getStaffid());
		param.setDatetimes(date);
		Long id = approveService.insert(param);
		if(null != id){
			approveService.approvalBilSqll(id, param.getType(),param.getState());
			for (int i = 0; i < staffids.length; i++) {
				ApproveDetail approveDetail = new ApproveDetail();
				approveDetail.setAid(id);
				approveDetail.setPid(staffids[i]);
				/** 审批状态  -1未审批，0待审批，1审批通过 2审批驳回*/
				if(i ==0){
					approveDetail.setState("0");
				}else{
					approveDetail.setState("-1");
				}
				approveDetail.setGrade(i+1);
				approveDetailService.insert(approveDetail);
			}
		}
		ResponseUtil.write(response, id);
	}
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/update")
	public void update(Approve param,HttpServletResponse response) throws Exception{
		Integer count = approveService.update(param);
		ResponseUtil.write(response,count);
	}
	/** 
	 * 根据主键撤销审批
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	 */
	@RequestMapping("/revoke")
	public void revoke(Long aid,HttpServletResponse response) throws Exception{
		Approve param = approveService.queryById(aid);
		Integer count = approveService.delete(String.valueOf(aid));
		if(count > 0){
			//修改对应单据状态
			approveService.approvalBilSqll(aid, param.getType(),-1);
		}
		ResponseUtil.write(response,count);
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	@RequestMapping("/delete")
	public void delete(String ids,HttpServletResponse response) throws Exception{
		ids=ids.replace("'",""); 
		Integer count = approveService.delete(ids);
		ResponseUtil.write(response,count);
	}
}
